package com.hjs.servlet.road;

import com.hjs.mapper.RoadAuditMapper;
import com.hjs.mapper.RoadMapper;
import com.hjs.mapper.RoadTypeMapper;
import com.hjs.mapper.RoadwayMapper;
import com.hjs.tools.Mapper;
import com.hjs.tools.RequestUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author flamemaster
 * @date 2020/6/19
 **/
@Slf4j
@WebServlet("/road/delete")
public class Delete extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response){
        RequestUtil requestUtil = new RequestUtil(request, response);
        switch (Integer.parseInt(request.getParameter("deleteType"))) {
            case 1:
                deleteRoadBase(requestUtil);
                break;
            case 2:
                deleteRoadway(requestUtil);
                break;
            case 3:
                deleteRoadType(requestUtil);
                break;
            case 4:
                deleteMaintenanceLevel(requestUtil);
                break;
            case 5:
                deleteAffiliatedFacility(requestUtil);
                break;
            case 6:
                deleteDividerOneSide(requestUtil);
                break;
            case 7:
                deleteSidewalkOneSide(requestUtil);
                break;
            case 8:
                deleteAudit(requestUtil);
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + Integer.parseInt(request.getParameter("addType")));
        }
    }


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response){

    }

    private void deleteRoadBase(RequestUtil requestUtil) {
        Mapper<RoadMapper> roadMapperMapper = new Mapper<>(RoadMapper.class);
        try {
            RoadMapper mapper = roadMapperMapper.getMapper();
            boolean result = mapper.deleteOneRoadById(requestUtil.getInt("roadID"));
            requestUtil.responseWrite("{\"result\":" + result + "}");
        } catch (Exception e) {
            e.printStackTrace();
            requestUtil.responseWrite("{\"result\": false + }");
        } finally {
            roadMapperMapper.closeSession();
        }
    }

    private void deleteRoadway(RequestUtil requestUtil) {

        try (SqlSession sqlSession = Mapper.getSqlSession()) {
            RoadwayMapper mapper = sqlSession.getMapper(RoadwayMapper.class);
            boolean sqlResult = mapper.deleteOneRoadwayById(requestUtil.getInt("roadwayID"));
            requestUtil.responseWrite("{\"result\":"+ "车行道-" + sqlResult + "}");
            RoadMapper mapper1 = sqlSession.getMapper(RoadMapper.class);
            sqlResult=mapper1.updateRoadRoadwayID(requestUtil.getInt("roadID"),null);
            requestUtil.responseWrite("{\"result\":"+ "道路基本信息同步更新-" + sqlResult + "}");
            Mapper.SessionEnd(sqlResult, sqlSession);
            requestUtil.responseIsSuccess(sqlResult,"成功！");
        } catch (Exception e) {
            log.error(e.toString());
            requestUtil.responseFail("未知原因失败！");
        }


    }


    private void deleteRoadType(RequestUtil requestUtil) {
        Mapper<RoadTypeMapper> roadTypeMapperMapper = new Mapper<>(RoadTypeMapper.class);
        try {
            RoadTypeMapper mapper = roadTypeMapperMapper.getMapper();
            boolean result = mapper.deleteOneRoadTypeById(requestUtil.getInt("roadTypeID"));
            requestUtil.responseWrite("{\"result\":" + result + "}");
        } catch (Exception e) {
            e.printStackTrace();
            requestUtil.responseWrite("{\"result\": false + }");
        } finally {
            roadTypeMapperMapper.closeSession();
        }
    }

    private void deleteMaintenanceLevel(RequestUtil requestUtil) {
        Mapper<RoadTypeMapper> roadTypeMapperMapper = new Mapper<>(RoadTypeMapper.class);
        try {
            RoadTypeMapper mapper = roadTypeMapperMapper.getMapper();
            boolean result = mapper.deleteOneMaintenanceLevelById(requestUtil.getInt("maintenanceLevelID"));
            requestUtil.responseWrite("{\"result\":" + result + "}");
        } catch (Exception e) {
            e.printStackTrace();
            requestUtil.responseWrite("{\"result\": false + }");
        } finally {
            roadTypeMapperMapper.closeSession();
        }
    }

    private void deleteAffiliatedFacility(RequestUtil requestUtil) {
        Mapper<RoadMapper> roadMapperMapper = new Mapper<>(RoadMapper.class);
        try {
            RoadMapper mapper = roadMapperMapper.getMapper();
            boolean result = mapper.deleteOneAffiliatedFacilityById(requestUtil.getInt("affiliatedFacilityID"));
            mapper.updateRoadAffiliatedFacilityID(requestUtil.getInt("roadID"),null);
            requestUtil.responseWrite("{\"result\":" + result + "}");
        } catch (Exception e) {
            e.printStackTrace();
            requestUtil.responseWrite("{\"result\": false + }");
        } finally {
            roadMapperMapper.closeSession();
        }
    }

    private void deleteDividerOneSide(RequestUtil requestUtil) {
        String[] properties = { "position"};
        try (SqlSession sqlSession = Mapper.getSqlSession()) {
            RoadMapper mapper = sqlSession.getMapper(RoadMapper.class);
            boolean flag = true;
            for (int i = 0; i < requestUtil.getInt("Number"); i++) {
                boolean sqlResult = mapper.deleteOneDividerOneSideById(requestUtil.getInt("roadID"),
                        requestUtil.getInt(properties[0] + i));
                if (!sqlResult) {
                    flag = false;
                    requestUtil.responseWrite("{\"result\": false + }");
                    sqlSession.rollback();
                    break;
                }
            }
            if (flag) {
                sqlSession.commit();
                requestUtil.responseWrite("{\"result\":" + true + "}");
            }
        } catch (Exception e) {
            e.printStackTrace();
            requestUtil.responseWrite("{\"result\": false + }");
        }

    }


    private void deleteSidewalkOneSide(RequestUtil requestUtil) {
        try (SqlSession sqlSession = Mapper.getSqlSession()) {
            RoadMapper mapper = sqlSession.getMapper(RoadMapper.class);
            boolean flag = true;
            for (int i = 0; i < requestUtil.getInt("Number"); i++) {
                boolean sqlResult = mapper.deleteOneSidewalkOneSideById(requestUtil.getInt("roadID"),
                        requestUtil.getInt("position" + i));
                if (!sqlResult) {
                    flag = false;
                    requestUtil.responseWrite("{\"result\": false + }");
                    sqlSession.rollback();
                    break;
                }
            }
            if (flag) {
                sqlSession.commit();
                requestUtil.responseWrite("{\"result\":" + true + "}");
            }
        } catch (Exception e) {
            e.printStackTrace();
            requestUtil.responseWrite("{\"result\": false + }");
        }
    }


    private void deleteAudit(RequestUtil requestUtil) {
        Mapper<RoadAuditMapper> roadAuditMapperMapper = new Mapper<>(RoadAuditMapper.class);
        try {
            RoadAuditMapper mapper = roadAuditMapperMapper.getMapper();
            boolean result = mapper.deleteById(requestUtil.getInt("roadAuditID"));
            requestUtil.responseWrite("{\"result\":" + result + "}");
        } catch (Exception e) {
            e.printStackTrace();
            requestUtil.responseWrite("{\"result\": false + }");
        } finally {
            roadAuditMapperMapper.closeSession();
        }
    }
}
